package com.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mall.common.constant.OrderConstant;
import com.mall.common.dto.OrderNoteDto;
import com.mall.common.service.impl.CrudServiceImpl;
import com.mall.ware.dao.WareOrderTaskDao;
import com.mall.ware.dto.WareOrderTaskDTO;
import com.mall.ware.dto.WareSkuDTO;
import com.mall.ware.entity.WareOrderTaskDetailEntity;
import com.mall.ware.entity.WareOrderTaskEntity;
import com.mall.ware.service.WareOrderTaskDetailService;
import com.mall.ware.service.WareOrderTaskService;
import com.mall.ware.service.WareSkuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 库存工作单
 *
 * @author xjc xjc@163.com
 * @since 1.0.0 2022-07-17
 */
@Slf4j
@Service
public class WareOrderTaskServiceImpl extends CrudServiceImpl<WareOrderTaskDao, WareOrderTaskEntity, WareOrderTaskDTO> implements WareOrderTaskService {

    @Resource
    private WareOrderTaskDetailService wareOrderTaskDetailService;
    @Resource
    private WareSkuService wareSkuService;

    @Override
    public QueryWrapper<WareOrderTaskEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<WareOrderTaskEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public WareOrderTaskEntity getOrderTaskByOrderSn(String orderSn) {
        QueryWrapper<WareOrderTaskEntity> queryWrapper = new QueryWrapper<WareOrderTaskEntity>().eq("order_sn", orderSn);
        return baseDao.selectOne(queryWrapper);
    }

    @Transactional
    @Override
    public void reduceStock(String orderSn) throws RuntimeException {
        QueryWrapper<WareOrderTaskEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_sn",orderSn);
        List<WareOrderTaskEntity> taskEntities = baseDao.selectList(queryWrapper);
        if (taskEntities!=null) {
            List<Long> taskIds = taskEntities.stream().map(WareOrderTaskEntity::getId).collect(Collectors.toList());
            List<WareOrderTaskDetailEntity> detailEntities = wareOrderTaskDetailService.listByTaskId(taskIds);
            List<WareOrderTaskDetailEntity> detailEntityList = detailEntities.stream().map(item -> {
                wareSkuService.reduceStock(item.getWareId(), item.getSkuId(), item.getSkuNum());
                WareOrderTaskDetailEntity detail = new WareOrderTaskDetailEntity();
                detail.setId(item.getId());
                detail.setLockStatus(2L);
                return detail;
            }).collect(Collectors.toList());
            log.info("待更新的库存详情数据"+detailEntityList.toString());
            wareOrderTaskDetailService.updateBatchById(detailEntityList);
        }
    }
}